<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function sendTemplateSMS($rest,$to,$datas,$tempId)
{
    $accountSid = config('sms.accountSid');
    $accountToken = config('sms.accountToken');
    $appId = config('sms.appId');
    $rest->setAccount($accountSid,$accountToken);
    $rest->setAppId($appId);

    $result = $rest->sendTemplateSMS($to,$datas,$tempId);
    if($result == NULL)
    {
        return "result error!";
    }
    if($result->statusCode!=0)
    {
        return "error code :" . $result->statusCode . " , error msg :" . $result->statusMsg;
    }else
    {
        return true;
    }
}

function Tree($data,$pid){
    $nav_data = [];
    foreach($data as $k=>$value)
    {
        if($value['pid'] == $pid)
        {
            $nav_data[$k] = $value;

            //$value['child'] = getTree($data,$value['id']);
            $nav_data[$k]['childs'] = Tree($data,$value['id']);
//            if(!empty($value['child']))
//            {
//                $nav_data[$k]['child']  = $value['child'];
//            }
        }
    }
    return $nav_data;
}

function getTree($data,$pid){
    $nav_data = [];
    foreach($data as $k=>$value)
    {
        if($value['parent_id'] == $pid)
        {
            $nav_data[$k] = $value;

            //$value['child'] = getTree($data,$value['id']);
            $nav_data[$k]['childs'] = getTree($data,$value['region_id']);
//            if(!empty($value['child']))
//            {
//                $nav_data[$k]['child']  = $value['child'];
//            }
        }
    }
    return $nav_data;
}

function getTreeOne($data,$pid,$s=''){
    $nav_data = [];
    foreach($data as $k=>$value)
    {
        if($value['pid'] == $pid)
        {
            $value['s'] = $s;
            $nav_data[$k] = $value;
            $child = getTreeOne($data,$value['id'],$s.'**');
            foreach($child as $v)
            {
                $nav_data[] = $v;
            }
        }
    }
    return $nav_data;
}

//1方法里的俩参数
function  getTree2($data,$pid){
    //2空变量
    $nav_data = [];
    $index = 0;
    //3循环第一级
    foreach($data as $k=>$value){
        //4判断第一级里的pid=  $pid 顶级0
        if($value['pid']==$pid){
            //5顶级数据
            $nav_data[$index]['title']=$value['title'];
            $nav_data[$index]['id']=$value['id'];
            // halt($nav_data);
            //6第二级的数据
            $value["children"]= getTree2($data,$value["id"]);
            if(!empty($value["children"])){
                //7 把第二级数据字段放在第一级里合并成一个数组
                $nav_data[$index]["children"]=$value["children"];
            }
            $index++;
        }
    }
    return $nav_data;
}